package com.jerei.utils;

import com.alibaba.fastjson.JSONObject;
import com.jerei.utils.condition.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/11/3.
 * 条件工具
 */
public class UtilsCondition {

    /**
     * 将request中的条件解析出来，转换成条件组，传送给服务层，服务层再传给dao层，由dao层转换成sql语句进行执行
     *
     * @param parameterMap HTTP的request请求
     * @return
     */
    public static ConditionEntity getConditionEntity(Map<String, String[]> parameterMap) {
        List<WhereEntity> whereEntities = new ArrayList<WhereEntity>();
        List<OrderByEntity> orderByEntities = new ArrayList<OrderByEntity>();
        if (null != parameterMap && parameterMap.size() > 0) {
            String[] valueArr = null;
            String valueStr = null;
            JSONObject obj = null;
            String _name = "";
            String __name = "";
            String _value = "";
            String __value = "";
            String prefix = "search.";
            String orderPrefix = "orderby.";
            String _prefix = "start.";
            WhereEntity whereEntity = null;
            OrderByEntity orderByEntity = null;
            for (String name : parameterMap.keySet()) {
                //如果是search.开头的，进行条件解析
                if (name.startsWith(prefix)) {
                    _name = name.substring(prefix.length());
                    valueArr = parameterMap.get(name);
                    if (null != valueArr && valueArr.length > 0) {
                        valueStr = valueArr[0];
                    } else {
                        valueStr = "";
                    }
                    //如果是between，则取出来end的部分
                    if (_name.startsWith("between")) {
                        //取出start或者end标识
                        __name = _name.substring("between".length() + 1);
                        //如果以start开头
                        if (__name.startsWith(_prefix)) {
                            _value = UtilsCommon.getString(parameterMap.get(name)[0]);
                            __value = UtilsCommon.getString(parameterMap.get(name.replaceAll(".start.", ".end."))[0]);
                            whereEntity = new WhereEntity();
                            whereEntity.setField(__name.substring(__name.lastIndexOf(".") + 1));
                            whereEntity.setOp(ConditionOp.between);
                            whereEntity.setValue(_value);
                            whereEntity.setValueEnd(__value);
                            whereEntities.add(whereEntity);
                        } else {
                            continue;
                        }
                    } else {
                        whereEntity = new WhereEntity();
                        whereEntity.setField(name.substring(name.lastIndexOf(".") + 1));
                        whereEntity.setOp(ConditionOp.valueOf(_name.substring(0, _name.lastIndexOf("."))));
                        whereEntity.setValue(valueStr);
                        whereEntities.add(whereEntity);
                    }
                } else if (name.startsWith(orderPrefix)) {//如果是排序开头
                    _name = name.substring(orderPrefix.length() + 1);
                    valueArr = parameterMap.get(name);
                    if (null != valueArr && valueArr.length > 0) {
                        valueStr = valueArr[0];
                    } else {
                        valueStr = "";
                    }
                    orderByEntity = new OrderByEntity();
                    orderByEntity.setOrder(_name);
                    orderByEntity.setOp(OrderByOp.valueOf(valueStr));
                    orderByEntities.add(orderByEntity);
                } else if (name.equals("sort")) {
                    _value = UtilsCommon.getString(parameterMap.get("sort")[0]);
                    orderByEntity = new OrderByEntity();
                    orderByEntity.setOrder(_value);
                    _value = UtilsCommon.getString(parameterMap.get("order")[0]);
                    orderByEntity.setOp(OrderByOp.valueOf(_value));
                    orderByEntities.add(orderByEntity);
                }
            }
        }
        ConditionEntity conditionEntity = new ConditionEntity();
        conditionEntity.setWhereEntities(whereEntities);
        conditionEntity.setOrderByEntities(orderByEntities);
        return conditionEntity;
    }
}